package com.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyCode.feature.mybaits.CustomPage;
import com.manager.client.SubCampusClient;
import com.manager.entity.dao.Manager;
import com.manager.entity.dao.SubCampus;
import com.manager.entity.dto.SubCampusDto;
import com.manager.entity.vo.SubCampusVo;
import com.manager.exception.CampusException;
import com.manager.exception.ManagerException;
import com.manager.mapper.SubCampusMapper;
import com.manager.service.ISubCampusService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.manager.entity.factory.SubCampusFactory.SUBCAMPUS_CONVERTER;

@Service
@RequiredArgsConstructor
public class SubCampusServiceImpl extends ServiceImpl<SubCampusMapper, SubCampus> implements ISubCampusService {
    @Autowired
    private SubCampusClient subCampusClient;

    @Override
    public Integer saveSubCampus(SubCampusVo subcampusVo) {
        //普通管理员只能在自己所处主校区下创建分校区
        if (!Manager.isSuperManager() && !Manager.getCurrentVisit().getCId().equals(subcampusVo.getParentId())) {
            throw new CampusException("权限不足");
        }
        SubCampus subcampus = SUBCAMPUS_CONVERTER.toSubCampus(subcampusVo);
        save(subcampus);
        return subcampus.getId();
    }

    @Override
    public SubCampusDto getSubCampusById(Integer id) {
        SubCampus subcampus = getById(id);
        if(subcampus==null){
            return null;
        }
        //普通管理员只能查询自己主校区的分校区
        if (!Manager.isSuperManager() && !Manager.getCurrentVisit().getCId().equals(subcampus.getParentId())) {
            return null;
        }
        return SUBCAMPUS_CONVERTER.toSubCampusDto(subcampus);
    }

    @Override
    public List<SubCampusDto> listSubCampus(CustomPage customPage) {
        List<SubCampus> subcampusList = null;
        //普通管理员: 过滤掉非自己主校区下分校区
        if (Manager.isSuperManager()) {
            subcampusList = CustomPage.getResult(customPage, new SubCampus(), this, null);
        } else {
            subcampusList = CustomPage.getResult(customPage, new SubCampus(), this, new QueryWrapper<SubCampus>()
                    .eq("parent_id", Manager.getCurrentVisit().getCId()));
        }
        return SUBCAMPUS_CONVERTER.toListSubCampusDto(subcampusList);
    }

    @Override
    public void delSubCampusById(Integer id) {
        //普通管理员只能删除自己主校区的分校区
        if (!Manager.isSuperManager() && !Manager.getCurrentVisit().getCId().equals(getById(id).getParentId())) {
            throw new CampusException("权限不足");
        }
        if (subCampusClient.existCourseUnderSubCampus(id)) {
            throw new CampusException("分校区关联着课程,无法删除");
        }
        removeById(id);
    }

    @Override
    public Boolean existSubCampusUnderCampus(Integer campusId, Integer subCampusId) {
        return count(new QueryWrapper<SubCampus>().eq("id", subCampusId).eq("parent_id", campusId)) == 1;
    }

    @Override
    public void checkExist(Integer cId) {
        long count = count(new QueryWrapper<SubCampus>().eq("id", cId));
        if (count == 0) {
            throw new ManagerException("子校区不存在");
        }
    }
}
